"use strict";

//var myApp = angular.module('myApp.controllers');

//myApp.controller('ConsultCtrl',function($scope, $http, $location, $routeParams) {
function AudioCtrl($scope, $http, $location, $routeParams,PatientServices,MedecinServices) {
	
	$scope.patient = PatientServices.getGlobalPatient();
	
	$scope.getPatient = function(name,surname,id) {
		PatientServices.readData(name,surname,id,function(data,status){(status==200)?$scope.patient=data:alert("An error occurred"); });
	};
	
	$scope.updateConsult = function(consultation) {
		PatientServices.setGlobalPatient($scope.patient);
	}
	
	$scope.getChartObject = function () {
		if (!$scope.patient.audiometries) return;
		
		for (var audiometrie of $scope.patient.audiometries) {
			audiometrie.vocal_droite = [];
			audiometrie.vocal_gauche = [];
			for (var j in audiometrie.vocal) {
				if (j < 21) {
					audiometrie.vocal_droite.push(audiometrie.vocal[j]);
				}
				else {
					audiometrie.vocal_gauche.push(audiometrie.vocal[j]);
				}
			};
		}
		
		var options = { lineWidth: 4, pointSize: 8 }; 
		var audio_options = { lineWidth: 4, pointSize: 8 };
		audio_options.vAxis = { viewWindow: { min: 0, max: 120 }, direction: -1};
	
		$scope.chartAudio_gauche = {
			  options: audio_options,
			  type: "LineChart",
			  data:
			  {
				"cols": [
				  {id: "audio_g_date", label: "Frequence", type: "string"}
				],
				"rows": []
			  }	
		};
		$scope.chartAudio_gauche.options.title = 'Audio Chart Gauche';

		
		$scope.chartAudio_droite = {
			  options: audio_options,
			  type: "LineChart",
			  data:
			  {
				"cols": [
				  {id: "audio_d_date", label: "Frequence", type: "string"}
				],
				"rows": []
			  }
		};
		$scope.chartAudio_droite.options.title = 'Audio Chart Droite';
	
		var av_options = { lineWidth: 4, pointSize: 8 };
		av_options.vAxis = { viewWindow: { min: 0, max: 120 } };
		$scope.chartAV_gauche = {
			  options: av_options,
			  type: "LineChart",
			  data:
			  {
				"cols": [
				  {id: "av_g_date", label: "Date", type: "string"}
				],
				"rows": []
			  }
		};
		$scope.chartAV_gauche.options.title = 'AV Chart';
		
		$scope.chartAV_droite = {
			  options: av_options,
			  type: "LineChart",
			  data:
			  {
				"cols": [
				  {id: "av_d_date", label: "Date", type: "string"}
				],
				"rows": []
			  }
		};
		$scope.chartAV_droite.options.title = 'AV Chart Droite';		
				
		var vocal_options = { lineWidth: 4, pointSize: 8 };
		vocal_options.vAxis = { viewWindow: { min: 0, max: 10 } };
		$scope.chartVocal = {
			  options: vocal_options,
			  type: "LineChart",
			  data:
			  {
				"cols": [
				  {id: "av_date", label: "Date", type: "string"}
				],
				"rows": []
			  }
		};
		$scope.chartVocal.options.title = 'Vocal Chart';

		for (var audiometrie of $scope.patient.audiometries)
		{
			var firstElement=false;
			if ( audiometrie.date != $scope.dateFilter) continue;
			else firstElement=true;
			//$scope.chartAudio_gauche.data.rows.push({c: [audiometrie.date]});
			$scope.chartAudio_gauche.data.cols.push({id: "audio_ag_"+audiometrie.date, label: "Aerien Gauche "+audiometrie.date, type: "number"});
			$scope.chartAudio_gauche.data.cols.push({id: "audio_og_"+audiometrie.date, label: "Osseux Gauche "+audiometrie.date, type: "number"});
			$scope.chartAudio_droite.data.cols.push({id: "audio_ad_"+audiometrie.date, label: "Aerien Droite "+audiometrie.date, type: "number"});
			$scope.chartAudio_droite.data.cols.push({id: "audio_od_"+audiometrie.date, label: "Osseux Droite "+audiometrie.date, type: "number"});
			
			var j = 0;
			for (var audio of audiometrie.audio) {
				if (firstElement) {
					$scope.chartAudio_gauche.data.rows.push({c: [audio.frequence]});
					$scope.chartAudio_droite.data.rows.push({c: [audio.frequence]});					
				}
				var row_gauche = $scope.chartAudio_gauche.data.rows[j];
				var row_droite = $scope.chartAudio_droite.data.rows[j];
				j++;
				if (audio.aerien_gauche > -1) row_gauche.c.push({v: audio.aerien_gauche});
				if (audio.osseux_gauche > -1) row_gauche.c.push({v: audio.osseux_gauche});
				if (audio.aerien_droite > -1) row_droite.c.push({v: audio.aerien_droite});
				if (audio.osseux_droite > -1) row_droite.c.push({v: audio.osseux_droite});
			}


			//$scope.chartAV_gauche.data.rows.push({c: [audiometrie.date]});
			$scope.chartAV_gauche.data.cols.push({id: "av_ag_"+audiometrie.date, label: "Aerien Gauche "+audiometrie.date, type: "number"});
			$scope.chartAV_gauche.data.cols.push({id: "av_og_"+audiometrie.date, label: "Osseux Gauche "+audiometrie.date, type: "number"});
			$scope.chartAV_droite.data.cols.push({id: "av_ad_"+audiometrie.date, label: "Aerien Droite "+audiometrie.date, type: "number"});
			$scope.chartAV_droite.data.cols.push({id: "av_od_"+audiometrie.date, label: "Osseux Droite "+audiometrie.date, type: "number"});
			j = 0;
			for (var av of audiometrie.av) {
				if (firstElement) {
					$scope.chartAV_gauche.data.rows.push({c: [av.type]});
					$scope.chartAV_droite.data.rows.push({c: [av.type]});					
				}
				var row_gauche = $scope.chartAV_gauche.data.rows[j];
				var row_droite = $scope.chartAV_droite.data.rows[j];
				j++;
				row_gauche.c.push({v: av.aerien_gauche});
				row_gauche.c.push({v: av.osseux_gauche});
				row_droite.c.push({v: av.aerien_droite});
				row_droite.c.push({v: av.osseux_droite});
			}
		
			$scope.chartVocal.data.cols.push({id: "av_vd_"+audiometrie.date, label: "Vocal Droite "+audiometrie.date, type: "number"});
			$scope.chartVocal.data.cols.push({id: "av_vg_"+audiometrie.date, label: "Vocal Gauche "+audiometrie.date, type: "number"});
			//var i = 0;
			for (var j in audiometrie.vocal_droite) {
				if (audiometrie.vocal_droite[j] != " " || audiometrie.vocal_gauche[j]) {
					if (firstElement) {
						$scope.chartVocal.data.rows.push({c: [j*5]});
					}
				}
			}
			for (var j in audiometrie.vocal_droite) {
				if (audiometrie.vocal_droite[j] != " " || audiometrie.vocal_gauche[j] != " ") {
					$scope.chartVocal.data.rows[j].c.push({v: audiometrie.vocal_droite[j]});
					$scope.chartVocal.data.rows[j].c.push({v: audiometrie.vocal_gauche[j]});
				}
			}
		}
	};
	
	

	$scope.getChartObject();
	updateWorkingOn("patient-audio",PatientServices,MedecinServices);

};